#include "../Base/stdafx.h"
#include "../Base/DXDevice.h"
#include "EffectLib.h"
#include "EffectAnim.h"
#include "../Base/DirectInput.h"

EffectAnim::EffectAnim(const tstring& file)
{
	m_Effect = EFFECTLIB->CreateEffect(file);

	m_Tech = m_Effect->GetTechniqueByName("Render");

	m_WorldVar = m_Effect->GetVariableByName("gWorld")->AsMatrix();
	m_ViewInv = m_Effect->GetVariableByName("gViewInv")->AsMatrix();
	m_WVPVar = m_Effect->GetVariableByName("gWVP")->AsMatrix();
	m_ViewProjVar = m_Effect->GetVariableByName("gViewProj")->AsMatrix();

	m_LightPosVar = m_Effect->GetVariableByName("gPosLight")->AsVector();
	m_LightAmbientVar = m_Effect->GetVariableByName("gLightAmbient")->AsVector();
	m_LightDiffVar = m_Effect->GetVariableByName("gLightColor")->AsVector();
	m_LightSpecVar = m_Effect->GetVariableByName("gLightSpec")->AsVector();

	m_EyePosVar = m_Effect->GetVariableByName("gEyePosW")->AsVector();

	m_TextureVar = m_Effect->GetVariableByName("diffuseTexture")->AsShaderResource();
	m_BoneArray = m_Effect->GetVariableByName("gBoneMatrix")->AsMatrix();
}

EffectAnim::~EffectAnim()
{

}

void EffectAnim::SetMatrixen(const D3DXMATRIX& world, const D3DXMATRIX& view, const D3DXMATRIX& proj)
{
	m_View = view;
	m_Proj = proj;
	m_WorldVar->SetMatrix((float*)&world);
	D3DXMATRIX viewInv;
	D3DXMatrixInverse(&viewInv, 0, &view);
	m_ViewInv->SetMatrix((float*)&viewInv);
	m_WVPVar->SetMatrix((float*)&(world * view * proj));
	m_ViewProjVar->SetMatrix((float*)&(view * proj));
}

void EffectAnim::SetMatrixen(const D3DXMATRIX& world)
{
	m_WorldVar->SetMatrix((float*)&world);
	m_WVPVar->SetMatrix((float*)&(world * m_View * m_Proj));
}

void EffectAnim::SetTexture(const ID3D10ShaderResourceView* texture)
{
	m_TextureVar->SetResource(const_cast<ID3D10ShaderResourceView*>(texture));
}

void EffectAnim::SetLight( D3DXVECTOR3& pos, D3DXVECTOR3& dir, D3DXVECTOR4& ambient, D3DXVECTOR4& diffuse, D3DXVECTOR4& spec )
{
	m_LightPosVar->SetFloatVector((float*)&pos);
	m_LightDiffVar->SetFloatVector((float*)&diffuse);
	m_LightAmbientVar->SetFloatVector((float*)&ambient);
	m_LightSpecVar->SetFloatVector((float*)&spec);
}

void EffectAnim::SetEyePos( D3DXVECTOR3& pos )
{
	m_EyePosVar->SetFloatVector((float*)&pos);
}

ID3D10EffectTechnique* EffectAnim::GetTech() const
{
	return m_Tech;
}

void EffectAnim::SetBoneMatrix( const vector<D3DXMATRIX>& matrix )
{
	m_BoneArray->SetMatrixArray((float*)&matrix[0], 0, matrix.size());
}